﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Media.Imaging;
using NLog;

namespace Client.Processes
{
    internal class LoadBytesFromFile
    {
        protected Logger logger = LogManager.GetCurrentClassLogger();

        internal void Process(string filepath)
        {
            var fileInfo = new FileInfo(filepath);
            var image = new BitmapImage();
            byte[] bytes = null;

            try
            {
                long numBytes = fileInfo.Length;
                var fStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                var br = new BinaryReader(fStream);
                bytes = br.ReadBytes((int)numBytes);
                bytes = Resize(bytes, 200, 300);
            }
            catch (Exception exc)
            {
                logger.Error(exc.Message);
            }
            finally
            {
                OnResult(bytes);
            }
        }

        internal  byte[] Resize(byte[] bytes, int width, int height)
        {
            var tc = TypeDescriptor.GetConverter(typeof(Bitmap));
            var bmp = (Bitmap)tc.ConvertFrom(bytes);
            var resBmp = new Bitmap(bmp, new Size(width, height));
            return (byte[])TypeDescriptor.GetConverter(resBmp).ConvertTo(resBmp, typeof(byte[]));
        }
    

        internal event Action<byte[]> OnResult = delegate { };
    }
}
